package com.xhorse.lockplatform.comm.util;

import java.io.*;

public class LogFileUtil {

	/**
	 * 读取日志文件
	 */
	public static String readLogFile(String filePath) {
		File file = new File(filePath);
		InputStreamReader isr = null;
		BufferedReader br = null;
		String resultLine = "";
		StringBuilder sb = new StringBuilder();
		try {
			isr = new InputStreamReader(new FileInputStream(file));
			br = new BufferedReader(isr);
			while ((resultLine=br.readLine())!=null) {
				sb.append(resultLine);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
		} finally {
			try {
				isr.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();

	}

	/**
	 * 写入日志文件
	 * 
	 * @param content
	 *            要写入的内容
	 * @param targetFilePath
	 *            目标日志文件
	 */
	public static void writeLogFile(String content, String targetFilePath) {
		File file = new File(targetFilePath);
		BufferedWriter bw = null;
			try {
				if (!file.exists()) {
					file.createNewFile();
				}
				bw = new BufferedWriter(new FileWriter(file,true));//设置true就是追加
				bw.write("\r\n");
				bw.write(content);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					bw.flush();
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}

}
